<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>tolerance (decorator)</title>
<link rel="stylesheet" href="../../../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../index.html" title="Boost.Test">
<link rel="up" href="../testing_tool_ref.html" title="Reference API for writing tests">
<link rel="prev" href="decorator_timeout.html" title="timeout (decorator)">
<link rel="next" href="assertion_boost_level.html" title="BOOST_&lt;level&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="decorator_timeout.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="boost_test.utf_reference.testing_tool_ref.decorator_tolerance"></a><a class="link" href="decorator_tolerance.html" title="tolerance (decorator)">tolerance
        (decorator)</a>
</h4></div></div></div>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">FPT</span><span class="special">&gt;</span>
  <span class="identifier">tolerance</span><span class="special">(</span><span class="identifier">FPT</span> <span class="identifier">eps</span><span class="special">);</span>

<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">FPT</span><span class="special">&gt;</span>
  <span class="identifier">tolerance</span><span class="special">(</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">fpc</span><span class="special">::</span><span class="identifier">percent_tolerance_t</span><span class="special">&lt;</span><span class="identifier">FPT</span><span class="special">&gt;</span> <span class="identifier">eps</span><span class="special">)</span>
</pre>
<p>
          Decorator <code class="computeroutput"><span class="identifier">tolerance</span></code> specifies
          the default comparison tolerance for floating point type <code class="computeroutput"><span class="identifier">FTP</span></code> in the decorated test unit. The default
          tolerance only applies to a particular type, so it makes sense to provide
          more than one <code class="computeroutput"><span class="identifier">tolerance</span></code>
          decorator if we are comparing different floating point types. The variant
          with <code class="computeroutput"><span class="identifier">percent_tolerance</span></code>
          uses value
        </p>
<pre class="programlisting"><span class="identifier">eps</span> <span class="special">/</span> <span class="number">100</span></pre>
<p>
          as tolerance.
        </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
            For more details see the <a class="link" href="../../testing_tools/extended_comparison/floating_point.html" title="Floating point comparison">floating
            points comparison</a> section.
          </p></td></tr>
</table></div>
<h6>
<a name="boost_test.utf_reference.testing_tool_ref.decorator_tolerance.h0"></a>
          <span class="phrase"><a name="boost_test.utf_reference.testing_tool_ref.decorator_tolerance.example_descr"></a></span><a class="link" href="decorator_tolerance.html#boost_test.utf_reference.testing_tool_ref.decorator_tolerance.example_descr">Example:
          decorator tolerance</a>
        </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Code
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TEST_MODULE</span> <span class="identifier">decorator_13</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">test</span><span class="special">/</span><span class="identifier">included</span><span class="special">/</span><span class="identifier">unit_test</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">utf</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unit_test</span><span class="special">;</span>
<span class="keyword">namespace</span> <span class="identifier">fpc</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">test_tools</span><span class="special">::</span><span class="identifier">fpc</span><span class="special">;</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test1</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">utf</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="number">0.0005</span><span class="special">))</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test2</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">utf</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="number">0.005</span><span class="special">))</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test3</span><span class="special">,</span> <span class="special">*</span> <span class="identifier">utf</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="number">0.05F</span><span class="special">))</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test4</span><span class="special">,</span>
  <span class="special">*</span> <span class="identifier">utf</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="identifier">fpc</span><span class="special">::</span><span class="identifier">percent_tolerance</span><span class="special">(</span><span class="number">0.05</span><span class="special">)))</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="special">);</span>
<span class="special">}</span>

<span class="identifier">BOOST_AUTO_TEST_CASE</span><span class="special">(</span><span class="identifier">test5</span><span class="special">,</span>
  <span class="special">*</span> <span class="identifier">utf</span><span class="special">::</span><span class="identifier">tolerance</span><span class="special">(</span><span class="identifier">fpc</span><span class="special">::</span><span class="identifier">percent_tolerance</span><span class="special">(</span><span class="number">0.5</span><span class="special">)))</span>
<span class="special">{</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="special">);</span>
  <span class="identifier">BOOST_TEST</span><span class="special">(</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="special">);</span>
<span class="special">}</span>
</pre>
                </td></tr></tbody>
</table></div>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                  <p>
                    Output
                  </p>
                </th></tr></thead>
<tbody><tr><td>
<pre xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" class="table-programlisting"><span class="special">&gt;</span> <span class="identifier">decorator_13</span>
<span class="identifier">Running</span> <span class="number">5</span> <span class="identifier">test</span> <span class="identifier">cases</span><span class="special">...</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">9</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">0.001</span> <span class="special">!=</span> <span class="number">0</span><span class="special">].</span> <span class="identifier">Absolute</span> <span class="identifier">value</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[|</span><span class="number">0.001</span><span class="special">|</span> <span class="special">&gt;</span> <span class="number">0.0005</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">10</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test1"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1.1000000000000001</span> <span class="special">!=</span> <span class="number">1.101</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[</span><span class="number">0.000908265</span> <span class="special">&gt;</span> <span class="number">0.0005</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">21</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test3"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">0.001</span> <span class="special">!=</span> <span class="number">0</span><span class="special">].</span> <span class="identifier">Absolute</span> <span class="identifier">value</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[|</span><span class="number">0.001</span><span class="special">|</span> <span class="special">&gt;</span> <span class="number">0</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">22</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test3"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1.1000000000000001</span> <span class="special">!=</span> <span class="number">1.101</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">28</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test4"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">0.001</span> <span class="special">==</span> <span class="number">0.000</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">0.001</span> <span class="special">!=</span> <span class="number">0</span><span class="special">].</span> <span class="identifier">Absolute</span> <span class="identifier">value</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[|</span><span class="number">0.001</span><span class="special">|</span> <span class="special">&gt;</span> <span class="number">0.0005</span><span class="special">]</span>
<span class="identifier">test</span><span class="special">.</span><span class="identifier">cpp</span><span class="special">(</span><span class="number">29</span><span class="special">):</span> <span class="identifier">error</span><span class="special">:</span> <span class="identifier">in</span> <span class="string">"test4"</span><span class="special">:</span> <span class="identifier">check</span> <span class="number">1.100</span> <span class="special">==</span> <span class="number">1.101</span> <span class="identifier">has</span> <span class="identifier">failed</span> <span class="special">[</span><span class="number">1.1000000000000001</span> <span class="special">!=</span> <span class="number">1.101</span><span class="special">].</span> <span class="identifier">Relative</span> <span class="identifier">difference</span> <span class="identifier">exceeds</span> <span class="identifier">tolerance</span> <span class="special">[</span><span class="number">0.000908265</span> <span class="special">&gt;</span> <span class="number">0.0005</span><span class="special">]</span>

<span class="special">***</span> <span class="number">6</span> <span class="identifier">failures</span> <span class="identifier">are</span> <span class="identifier">detected</span> <span class="identifier">in</span> <span class="identifier">the</span> <span class="identifier">test</span> <span class="identifier">module</span> <span class="string">"decorator_13"</span>
</pre>
                </td></tr></tbody>
</table></div>
<p>
          In the above example, in <code class="computeroutput"><span class="identifier">test1</span></code>,
          checks on <code class="computeroutput"><span class="keyword">double</span></code>s fail because
          they differ by more what tolerance for <code class="computeroutput"><span class="keyword">double</span></code>s
          specifies. In <code class="computeroutput"><span class="identifier">test2</span></code> the
          tolerance for <code class="computeroutput"><span class="keyword">double</span></code>s is greater
          and therefore the checks succeed. In <code class="computeroutput"><span class="identifier">test3</span></code>,
          we specify only tolerance for type <code class="computeroutput"><span class="keyword">float</span></code>,
          and since the checks use type <code class="computeroutput"><span class="keyword">double</span></code>
          the specified tolerance does not apply. Tolerance in <code class="computeroutput"><span class="identifier">test4</span></code>
          is equivalent to that in <code class="computeroutput"><span class="identifier">test1</span></code>,
          therefore its checks also fail. Tolerance in <code class="computeroutput"><span class="identifier">test5</span></code>
          is equivalent to that in <code class="computeroutput"><span class="identifier">test2</span></code>,
          therefore its checks also succeed.
        </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2020 Boost.Test contributors<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="decorator_timeout.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../testing_tool_ref.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="assertion_boost_level.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
